package com.yilin.platform.base_core.commons.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.lang.Nullable;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class MapUtil {

    public interface Put<T,V>{
        void put(Map<T, V> map);
    }

    /**
     * 生成MAP参数
     * */
    public static <T,V> Map<T,V> generate(Put<T,V> put){
        Map<T,V> map = new HashMap<>();
        put.put(map);
        return map;
    }

    public static String getMapString(@Nullable Map<String, Object> map, String key) {
        if(map==null)  return  null;
        Object obj = map.get(key);
        if (obj != null) {
            return obj.toString();
        }
        return  null;
    }

    //生成授权件键值对
    public static Map<String, String[]> generateAuthorityMap(String[] menuCodesAndOperateCodes) {
        Map<String, String[]> menuCodesAndOperateMap = new HashMap<>();
        for (String menuCodesAndOperateCode : menuCodesAndOperateCodes) {
            String menuCode = menuCodesAndOperateCode.split(":")[0];
            menuCodesAndOperateMap.put(menuCode, menuCodesAndOperateCode.split(":")[1].split("&"));
        }
        return menuCodesAndOperateMap;
    }
    //json 转mpa
    public static Map<String, Object> jsonToMap(String json) {
        Type type = new TypeToken<Map<String, Object>>() {}.getType();
        return new Gson().fromJson(json, type);
    }

}


